package com.itheima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.SetmealService;
import com.itheima.bean.Params;
import com.itheima.bean.R;
import com.itheima.bean.Setmeal;
import com.itheima.dto.SetmealDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @GetMapping("/list")
    public R findUserSetmeal(Long categoryId,int status){
    List<Setmeal>  list=setmealService.findUserSetmeal(categoryId,status);
      return R.success(list);
    }


    @DeleteMapping
    public R delete(@RequestParam("ids") List<Long> list){

      int row=setmealService.delete(list);
      if (row==1){
          return R.success("套餐删除成功！");
      }return R.error("套餐删除成功！");
    }
    @PostMapping("/status/{status}")
     public R updateStatus(@PathVariable int status,@RequestParam("ids") List<Long> list){
//        System.out.println("===========" + status + "========" + Arrays.toString(ids));
        int row=setmealService.updateStatus(status,list);
       if (row>0){
           return R.success("状态修改成功！");
       }
       return R.error("状态修改失败！");
    }
    @PostMapping
    public R add(@RequestBody SetmealDto setmealDto){
     int row = setmealService.add(setmealDto);
     if (row==1){
         return R.success("新增套餐成功！");
     }return R.error("新增套餐失败！");
    }

    @GetMapping("/page")
    public R findPage(Params params){

      IPage<SetmealDto> dtoIPage= setmealService.findPage(params);
      return R.success(dtoIPage);
    }
}
